package Base.ArrayTset;

/*
 * @author 枫城
 * @createTime 2022/8/6 17:20
 * @desc 算法，数组的复制,反转，查找(二分查找)
 *     二分法查找前提：有序
 */

import java.util.Arrays;

public class ArrayTest {

    public static void main(String[] args) throws InterruptedException {
        String[] arr1 = new String[]{"A", "B", "C", "D", "E", "F"};
        int[] arr2 = new int[]{-15, 12, 23, 34, 45, 56, 67};
        copy(arr1);
        reverse(arr1);
        // 二分法查找前提：有序
        find(arr2);
    }

    // 复制
    public static void copy( String[] arr){
        final String[] newArr = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        System.out.println("复制结果："+Arrays.toString(newArr));
    }

    // 反转
    public static void reverse(String[] arr){
        for (int i = 0; i < arr.length/2; i++) {
            String temp = arr[i];
            arr[i] = arr[arr.length-i-1];
            arr[arr.length-i-1]=temp;
        }
        System.out.println("反转结果："+ Arrays.toString(arr));
    }

    // 查找
    public static void find(int[] arr) throws InterruptedException {
        int dest = 34;
        int head = 0;
        int end = arr.length-1;
        Boolean isFind = true;
        while (head <= end){
            int middle = (head+end)/2;
            System.out.println("==========");
            if (dest==arr[middle]){
                System.out.println("找到该元素位置为："+middle);
                isFind = false;
                break;
            }else if (dest<arr[middle]){
                end = middle - 1;
            }else{
                head = middle + 1;
            }
        }
        if (isFind){
            System.out.println("未找到！");
        }
    }


}
